import { Injectable } from '@nestjs/common';
import { NacosNamingClientService } from 'libs/nacos/nacosNamingClient.service';
import actionBuilder from 'libs/common/utils/actionBuilder';
import User from '../entity/user.entity';

@Injectable()
export default class RoleApi {
  private action = actionBuilder({
    timeout: 30 * 1000
  });
  constructor(
    private nacosNamingClientService: NacosNamingClientService
  ) {}

  private async getUserOneHostPath(){
    const host = await this.nacosNamingClientService.selectOneInstances({
      serviceName:'auth',
      groupName:'DEFAULT_GROUP'
    })
    const path = 'http://'+host.ip+':'+host.port
    return path
  }
  
  async getRoleById(roleId: string):Promise<string> {
    const path = await this.getUserOneHostPath()
    return await this.action({
      method: 'POST',
      url: '/role/getRoleById',
      data: {
        roleId
      },
      baseURL: path
    });
  }

  async getRoleByKey(roleKey: string):Promise<User> {
    const path = await this.getUserOneHostPath()
    return await this.action({
      method: 'POST',
      url: '/role/getRoleByKey',
      data: {
        roleKey
      },
      baseURL: path
    });
  }

  async getRoleByRoleIds(roleIds: string[]):Promise<string[]> {
    const path = await this.getUserOneHostPath()
    return await this.action({
      method: 'POST',
      url: '/role/getRoleByRoleIds',
      data: {
        roleIds
      },
      baseURL: path
    });
  }
}
